package com.landun.market.web.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.landun.market.pojo.Constants;
import com.landun.market.pojo.enums.StateCode;
import com.landun.market.pojo.model.Price;
import com.landun.market.util.FinanceUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.Date;

/**
 * Created by yi.zhao on 2018/5/19.
 */
@Data
@ToString
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class PriceVO extends BaseVO<PriceVO> {
    private Integer id;
    private Integer categoryId;
    private String categoryName;
    private String name;
    private String area;
    private String unit;
    private String minSellingPrice;
    private String maxSellingPrice;
    private String wholesalePrice;
    private String referencePrice;
    @JsonFormat(pattern = Constants.DATE_FORMAT, locale = "zh", timezone = "GMT+8")
    private Date priceDate;
    private StateCode state;
    private String stateDesc;

    public PriceVO(Price price) {
        if (price != null) {
            this.id = price.getId();
            this.categoryId = price.getCategoryId();
            this.name = price.getName();
            this.area = price.getArea();
            this.unit = price.getUnit();
            this.minSellingPrice = FinanceUtils.convertToYuan(price.getMinSellingPrice());
            this.maxSellingPrice = FinanceUtils.convertToYuan(price.getMaxSellingPrice());
            this.wholesalePrice = FinanceUtils.convertToYuan(price.getWholesalePrice());
            this.referencePrice = FinanceUtils.convertToYuan(price.getReferencePrice());
            this.priceDate = price.getPriceDate();
            this.state = price.getState();
            if (state != null) {
                this.stateDesc = state.desc();
            }
        }
    }

    public Price toEntity() {
        Price price = new Price();
        price.setId(this.id);
        price.setName(this.name);
        if (this.minSellingPrice != null) {
            price.setMinSellingPrice(FinanceUtils.getSysPrice(this.minSellingPrice));
        }
        if (this.maxSellingPrice != null) {
            price.setMaxSellingPrice(FinanceUtils.getSysPrice(this.maxSellingPrice));
        }
        if (this.referencePrice != null) {
            price.setReferencePrice(FinanceUtils.getSysPrice(this.referencePrice));
        }
        price.setUnit(this.getUnit());

        return price;
    }
}
